<template>
  <div>
    <el-form
      label-width="100px"
      :model="user"
      class="form"
      :rules="rules"
      ref="user"
    >
      <el-form-item label="昵称" class="item" prop="nickname">
        <el-input
          v-model="user.nickname"
          style="width: 350px"
          placeholder="请输入昵称"
        ></el-input>
      </el-form-item>
      <el-form-item label="账号" class="item" prop="username">
        <el-input
          v-model="user.username"
          style="width: 350px"
          placeholder="请输入账号"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" class="item" prop="password">
        <el-input
          v-model="user.password"
          style="width: 350px"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户组" class="item" prop="role">
        <el-select v-model="user.role" placeholder="请选择用户组">
          <el-option
            v-for="item in user.list"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="clickAdd('user')"
          >新增用户</el-button
        >
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { adduser } from "@/api/apis";
export default {
  data() {
    return {
      user: {
        username: "",
        nickname: "",
        role: "",
        password: "",
        list: [
          { label: "超级管理员", value: 2 },
          { label: "中级管理员", value: 1 },
          { label: "初级管理员", value: 0 },
        ],
        // uid: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        nickname: [
          { required: true, message: "昵称不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        role: [
          { required: true, message: "用户组别不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    clickAdd(user) {
      //   let { acc, pwd, usergroup } = this;
      this.$refs[user].validate((valid) => {
        if (valid) {
          adduser(this.user).then((res) => {
            //    console.log(res)
            if (res.data.code == 200) {
              this.$message({
                message: "恭喜你，账号添加成功",
                type: "success",
              });
              this.$router.push("/main/userlist");
            } else {
              this.$message.error("错了哦，账号添加失败");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    reset() {
      this.user.username = "";
      this.user.nickname = "";
      this.user.password = "";
      this.user.role = "";
    },
    // go() {
    //   this.$router.go(-1);
    // },
  },
  // mounted() {

  // },
};
</script>
<style scoped>
.form {
  margin-top: 50px;
  margin-left: 30px;
}
.item {
  margin-bottom: 30px;
}
</style>